<!DOCTYPE html>
<html>
  <head>
    <title>TEST CASE FOR COOKIES API</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node.js
    <script>
      var gui = require('nw.gui');
      var net = require('net');
      var win = gui.Window.get( window.open('http://115.com'));      
      var results = new Array();
      var changed = false;
      // action 1: retrieve(get), retrieveAll(getAll) set
      // action 2: set
      // action 3: set and onChanged
      var action = gui.App.argv[0]; 

      //retrieveAll(".115.com")
      function retrieveAll(domain) {
	  win.cookies.getAll({ 'domain': domain}, function(cookies) {
	      console.log(cookies);
	      if (cookies != null && cookies != undefined)
		  results.push(true);
	      else
		  results.push(false);
          //     for (var i = 0; i < cookies.length; i++) {
          //         console.log("name: " + cookies[i].name + "\n" + "path: " + cookies[i].path + "\n" + "domain: " + cookies[i].domain + "\n" + "session: " + cookies[i].session.toString() + "\n" + "value: " + cookies[i].value + " " + "\n");
	  //     }
	  })
      }

     //remove("http://www.115.com","115_lang")
     function remove(url, name) {
	 win.cookies.remove({ 'url': url, 'name': name });
     }

     //set('.115.com',"http://www.115.com","115_lang","en")
     function set(domain, url, name, value) {
	 win.cookies.set({ 'domain': domain, 'url': url, 'name': name, 'value': value, 'expirationDate': 9999999999});
     }

     //retrieve("http://www.115.com","115_lang")
     function retrieve(url, name){
	 win.cookies.get({"url":url, "name": name},function (cookie){
	     console.log(cookie);
	     if (cookie != null && cookie != undefined) {
		 results.push(true);
	     } else {
		 results.push(false);
	     }
	 });
     }

     function sendMessage(data) {
      var socket = net.connect({port: 13013});
      socket.setEncoding('utf8');
      if (data) {
        socket.write(JSON.stringify(data));
      }
      socket.end();
     }
     
     win.cookies.onChanged.addListener(function() {
	 changed = true;
     });
    
     if (action == '1') {
	 win.window.onload = function() {
	     retrieve("http://www.115.com","115_lang");//get true
     	     retrieveAll(".115.com");//getAll true
	     results.push(changed);
    	     win.close();
	     // true, true
	     setTimeout(function() {
          sendMessage(results);

	     }, 1000);
	 }
     } else if (action == '2') {
	 win.close();
     	 set('.115.com',"http://www.115.com","115_lang","en");//set true
	 win = gui.Window.get(window.open('http://115.com'));      
	 win.window.onload = function() {
    	     if (win.window.document.title != "115 Cloud drive-Your files in the cloud-Easy&Safe")
    		 results.push(false);
	     else
		 results.push(true);
	     results.push(changed);
	     win.close();
	     setTimeout(function() {
            sendMessage(results);
	     }, 2000)
	 }
     } else if (action == '3') {
	 win.close();
     	 set('.115.com',"http://www.115.com","115_lang","zh");//set true
	 win = gui.Window.get(window.open('http://115.com'));      
	 win.window.onload = function() {
	     if (win.window.document.title == "115 Cloud drive-Your files in the cloud-Easy&Safe")
    		 results.push(false);
	     else
		 results.push(true);
	     setTimeout(function() {
		 win.close();	    
		 results.push(changed);
	     }, 1000);
	     // true
	     setTimeout(function() {
            sendMessage(results);
	     }, 3000);
	 }
     } else if (action == '4') {
	 remove("http://www.115.com","115_lang");
	 results.push(changed);
         retrieve("http://www.115.com","115_lang");//get false
	 
	 //false, true
	 setTimeout(function() {
        sendMessage(results);
	 }, 1000);
     }

    
    </script>
  </body>
</html>
